package com.framework.disconf.config;

import com.baidu.disconf.client.DisconfMgrBean;
import com.baidu.disconf.client.DisconfMgrBeanSecond;
import com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class DisconfConfig {

    @Bean(destroyMethod = "destroy")
    @AutoConfigureOrder
    public DisconfMgrBean disconfMgrBean(){
        DisconfMgrBean disconfMgrBean = new DisconfMgrBean();
        disconfMgrBean.setScanPackage("com.framework.disconf");
        return disconfMgrBean;
    }

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public DisconfMgrBeanSecond disconfMgrBeanSecond(){
        return new DisconfMgrBeanSecond();
    }

    @Bean("propertiesFactoryBean")
    public ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean(){
        ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean = new ReloadablePropertiesFactoryBean();
        List<String> locations = new ArrayList<>();
        locations.add("classpath*:application.properties");
        reloadablePropertiesFactoryBean.setLocations(locations);
        return reloadablePropertiesFactoryBean;
    }

    @Bean("propertyPlaceholderConfigurer")
    @Order(-2147483648)
    public DisconfPropertiesSpringInit disconfPropertiesSpringInit(@Qualifier("propertiesFactoryBean") ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean, ConfigurableEnvironment environment){
        return new DisconfPropertiesSpringInit(reloadablePropertiesFactoryBean, environment);
    }
}
